package com.ellwood.copyright.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ellwood.copyright.common.core.annotation.Master;
import com.ellwood.copyright.common.core.base.ServiceImplFactory;
import com.ellwood.copyright.common.core.constant.Constant;
import com.ellwood.copyright.common.core.enums.HttpCodeEnum;
import com.ellwood.copyright.common.core.exception.ServerException;
import com.ellwood.copyright.common.core.result.EllPageResult;
import com.ellwood.copyright.common.core.utils.EllAESUtil;
import com.ellwood.copyright.common.core.utils.excel.ExcelExportUtil;
import com.ellwood.copyright.common.core.utils.excel.ExcelImportUtil;
import com.ellwood.copyright.infrastructure.system.entity.SysUserEntity;
import com.ellwood.copyright.infrastructure.system.mapper.SysUserMapper;
import com.ellwood.copyright.infrastructure.system.query.SysUserQuery;
import com.ellwood.copyright.infrastructure.system.vo.SysUserDataVO;
import com.ellwood.copyright.modules.system.convert.SysUserConvert;
import com.ellwood.copyright.modules.system.logic.SysUserLogic;
import com.ellwood.copyright.modules.system.service.SysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.modules.system.service.impl
 * @ClassName: SysUserServiceImpl
 * @Description: // 用户service接口实现
 * @Author: Ellwood
 * @CreateDate: 2024/11/20 - 22:42
 * @Version: V1.0.0
 */
@Slf4j
@Master
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends ServiceImplFactory<SysUserMapper, SysUserEntity> implements SysUserService {

    private SysUserLogic sysUserLogic;

    @Override
    public EllPageResult<SysUserDataVO> findAll(SysUserQuery userQuery) {
        // 查询参数
        Map<String, Object> params = sysUserLogic.findAllParams(userQuery);
        // 分页查询
        IPage<SysUserEntity> page = getPage(userQuery);
        params.put(Constant.PAGE,page);
        // 数据列表
        List<SysUserEntity> userList=baseMapper.findAll(params);
        return new EllPageResult<>(SysUserConvert.INSTANCE.converList(userList),page.getTotal());
    }

    @Override
    public int userSave(SysUserDataVO userVo) {
        SysUserEntity params = sysUserLogic.getUserParams(userVo);

        // 密码处理->暂时这么对密码进行加密处理，等加入权限控制之后再进行逻辑处理
//        System.out.println("加密 = " + EllAESUtil.encrypt("123456"));
//        System.out.println("解密 = " + EllAESUtil.decrypt(" 5npjufQ0AT0RkEDe6Rcnsw"));
        params.setPassword(EllAESUtil.encrypt("123456"));

        return baseMapper.insert(params);
    }

    @Override
    public int editUser(SysUserDataVO userVo) {
        SysUserEntity params=sysUserLogic.getUserParams(userVo);
        return baseMapper.updateById(params);
    }

    @Override
    public void exportByUsers(String filePath, String fileName) throws Exception {
        LambdaQueryWrapper<SysUserEntity> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(SysUserEntity::getIsState,0).eq(SysUserEntity::getIsDeleted,0);

        List<SysUserEntity> userEntityList = baseMapper.selectList(userWrapper);
        List<SysUserDataVO> userDataVOS = SysUserConvert.INSTANCE.converList(userEntityList);

        String path=filePath+fileName;

        ExcelExportUtil.exportToExcel(userDataVOS, SysUserDataVO.class, path);

    }

    @Override
    public void importByUsers(MultipartFile file) throws Exception {
        List<SysUserDataVO> userDataVOS = ExcelImportUtil.importFromExcel(file, SysUserDataVO.class);

        // 清除导入数据中的 id 字段，因为数据库是自增的
        userDataVOS.forEach(vo->vo.setUserId(null));

        List<SysUserEntity> userEntities = SysUserConvert.INSTANCE.convertList(userDataVOS);

        userEntities.forEach(user->user.setPassword(EllAESUtil.encrypt("123456")));
        System.out.println("userEntities = " + userEntities);

        // 批量保存数据
        baseMapper.insert(userEntities);
    }

}
